//
//  QQChatViewController.m
//  MacDevTest
//
//  Created by lbxia on 2020/8/15.
//  Copyright © 2020 com.lbx. All rights reserved.
//

#import "QQChatViewController.h"
#import "LBXFriendsTableView.h"
#import <Masonry/Masonry.h>
#import "JJScroller.h"
#import "CustomRowView.h"
#import "FriendListView.h"

@interface QQChatViewController ()<NSTabViewDelegate,NSTableViewDataSource,FriendListViewDelegate>
@property (nonatomic,strong)  NSTableView *tableView;

@property (nonatomic, strong) NSMutableArray *arrayDatas;
@property (nonatomic, assign) NSInteger preMaxCount;

@property (weak) IBOutlet NSView *chatContentListBackView;
@property (weak) IBOutlet NSTextField *chattingFriendLabel;
@property (weak) IBOutlet NSView *chatInputAreaBackView;


@end

@implementation QQChatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    
    
    self.arrayDatas = [NSMutableArray array];
    self.preMaxCount = 20;
    for (int i = 0; i < self.preMaxCount; i++) {
        [self.arrayDatas addObject:[NSString stringWithFormat:@"%d",i]];
    }


    NSScrollView * scrollView    = [[NSScrollView alloc] init];
    scrollView.borderType = NSNoBorder;
    
    //自定义滚动条，没有背景色，静止时，自动隐藏
    scrollView.verticalScroller = [[JJScroller alloc]init];
    scrollView.hasVerticalScroller  = YES;
    
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.view);
        make.top.equalTo(self.view);
        make.width.mas_equalTo(200);
    }];
    
    self.tableView = [[NSTableView alloc]initWithFrame:self.view.bounds];
    NSTableColumn * column = [[NSTableColumn alloc]initWithIdentifier:@"col1"];
    column.title = @"col1";
    column.minWidth = 200;
    column.maxWidth = 200;
    column.headerCell = [[NSTableHeaderCell alloc]initTextCell:@"11"];
    column.headerCell.backgroundColor = [NSColor clearColor];
    [_tableView addTableColumn:column];
    
    //隐藏列表头 列的名称
    [_tableView setHeaderView:nil];
    
    scrollView.contentView.documentView = _tableView;
    
    //Assigning to 'id<NSTableViewDelegate> _Nullable' from incompatible type 'NormalTableView *const __strong'
    _tableView.delegate = (id)self;
    _tableView.dataSource = self;
    [_tableView reloadData];
    
    [self initChatView];
      
}

- (void)initChatView
{
    _chatContentListBackView.wantsLayer = YES;
    _chatContentListBackView.layer.backgroundColor = [NSColor whiteColor].CGColor;
    
    _chatInputAreaBackView.wantsLayer = YES;
    _chatInputAreaBackView.layer.backgroundColor = [NSColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:0].CGColor;
}

- (void)addfriend
{
    [self.arrayDatas addObject:[NSString stringWithFormat:@"%ld",++self.preMaxCount]];
    
    
    NSInteger selectedRow = [self.tableView selectedRow];
    
    
    [self.tableView reloadData];
    
    if ( selectedRow >=0  ) {
        [self.tableView selectRowIndexes:[[NSIndexSet alloc]initWithIndex:selectedRow] byExtendingSelection:NO];
        
    }
    
}

#pragma mark- delegate

//  - （BOOL）validateProposedFirstResponder：（NSResponder *）responder forEvent：（NSEvent *） event
// {
// //这允许用户点击单元格中的控件，首先必须选择单元格行
// return YES;
//}

- (BOOL)validateProposedFirstResponder:(NSResponder *)responder forEvent:(NSEvent *)event
{
    return YES;
}
 

- (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(nullable NSTabViewItem *)tabViewItem
{
    return YES;
}

- (void)tabView:(NSTabView *)tabView willSelectTabViewItem:(nullable NSTabViewItem *)tabViewItem
{
    NSLog(@"willSelectTabViewItem");
}

- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(nullable NSTabViewItem *)tabViewItem
{
    NSLog(@"didSelectTabViewItem :%@",tabViewItem.identifier);
}

- (void)tabViewDidChangeNumberOfTabViewItems:(NSTabView *)tabView
{
    NSLog(@"tabViewDidChangeNumberOfTabViewItems");
}

#pragma mark - NSTableViewDataSource
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return self.arrayDatas.count;
    
}

// 设置行高
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row{
    return 60;
}



- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
{
    //修改选中颜色
    CustomRowView *rowView = [[CustomRowView alloc] init];
    return rowView;
}

//view-based模式(基于视图)

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    
    FriendListView *fView = [[FriendListView alloc]initWithFrame:NSMakeRect(0, 0, 220, 40)];
    fView.delegate = self;
    fView.row = row;
    
    fView.userImageView.image = [NSImage imageNamed:@"personalImg"];
    
    fView.userNickName.stringValue = [NSString stringWithFormat:@"朋友昵称:%@",self.arrayDatas[row]];
    
    fView.detailInfo.stringValue = [NSString stringWithFormat:@"detaininfo"];

    return fView;
}

#pragma mark- cellViewDelegate

- (void)didselectRow:(NSInteger)row identifer:(NSString*)identifer view:(FriendListView*)view
{
    NSLog(@"左键 row:%ld",row);
    
    self.chattingFriendLabel.stringValue = [NSString stringWithFormat:@"朋友昵称:%@",self.arrayDatas[row]];
}

- (void)didRightMouseDownRow:(NSInteger)row identifer:(NSString*)identifer view:(FriendListView*)view
{
    NSLog(@"右键 row:%ld",row);
}

- (void)didCloseView:(NSInteger)row identifer:(NSString*)identifer view:(FriendListView*)view
{
    NSInteger selectedRow = [self.tableView selectedRow];

    
    [self.arrayDatas removeObjectAtIndex:row];
    
    if (selectedRow > 0 && selectedRow > row) {
        
        selectedRow -= 1;
    }
    
    [self.tableView reloadData];
    
    if ( selectedRow >=0  ) {
        [self.tableView selectRowIndexes:[[NSIndexSet alloc]initWithIndex:selectedRow] byExtendingSelection:NO];
        
    }
    
}


@end
